package com.zzs.judge.callback;

import com.github.dockerjava.api.async.ResultCallbackTemplate;
import com.github.dockerjava.api.model.Frame;
import com.github.dockerjava.api.model.StreamType;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;



/**
 * 自定义回调类，用于捕获 Docker 容器执行命令的输出和错误信息。
 * @author zzs
 * @date 2025/03/29
 */
@NoArgsConstructor
@Getter
@Setter
public class ExecResultCallback extends ResultCallbackTemplate<ExecResultCallback, Frame> {
    private StringBuilder stdout = new StringBuilder();
    private StringBuilder stderr = new StringBuilder();
    private boolean success = false;

    @Override
    public void onNext(Frame object) {
        System.out.println("执行中: " + System.currentTimeMillis());
        String output = new String(object.getPayload());
        if (object.getStreamType() == StreamType.STDOUT) {
            stdout.append(output);
        } else if (object.getStreamType() == StreamType.STDERR) {
            stderr.append(output);
        }
    }

    @Override
    public void onComplete() {
        System.out.println("执行结束: " + System.currentTimeMillis());
        success = stderr.toString().isBlank();
        super.onComplete();
    }

    public String getStdout() {
        return stdout.toString();
    }

    public String getStderr() {
        return stderr.toString();
    }
}
